<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>太空射击游戏（高难度版）</title>
    <style>
        body {
            margin: 0;
            overflow: hidden;
            background: linear-gradient(to bottom, #000033, #000066);
            font-family: Arial, sans-serif;
            text-align: center;
            user-select: none;
        }
        #gameArea {
            position: relative;
            width: 100vw;
            height: 100vh;
        }
        #player {
            position: absolute;
            width: 50px;
            height: 50px;
            background-color: #00FF00;
            clip-path: polygon(50% 0%, 0% 100%, 100% 100%);
            bottom: 50px;
            left: calc(50% - 25px);
            z-index: 10;
        }
        .enemy {
            position: absolute;
            width: 40px;
            height: 40px;
            background-color: #FF0000;
            clip-path: polygon(50% 0%, 100% 50%, 50% 100%, 0% 50%);
            z-index: 5;
        }
        .enemy-fast {
            position: absolute;
            width: 30px;
            height: 30px;
            background-color: #FFFF00;
            clip-path: polygon(50% 0%, 100% 50%, 50% 100%, 0% 50%);
            z-index: 5;
        }
        .enemy-boss {
            position: absolute;
            width: 60px;
            height: 60px;
            background-color: #FF00FF;
            clip-path: polygon(50% 0%, 100% 50%, 50% 100%, 0% 50%);
            z-index: 5;
        }
        .bullet {
            position: absolute;
            width: 5px;
            height: 15px;
            background-color: #FFFFFF;
            z-index: 8;
        }
        .enemy-bullet {
            position: absolute;
            width: 5px;
            height: 15px;
            background-color: #FF0000;
            z-index: 8;
        }
        #score {
            position: absolute;
            top: 20px;
            left: 20px;
            font-size: 24px;
            color: white;
            background: rgba(0,0,0,0.5);
            padding: 10px;
            border-radius: 5px;
        }
        #gameOver {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            font-size: 48px;
            color: red;
            background: rgba(255,255,255,0.8);
            padding: 20px;
            border-radius: 10px;
            display: none;
        }
    </style>
</head>
<body>
    <div id="gameArea">
        <div id="player"></div>
        <div id="score">分数: 0</div>
        <div id="gameOver">游戏结束!<br>按空格键重新开始</div>
    </div>

    <script>
        const gameArea = document.getElementById('gameArea');
        const player = document.getElementById('player');
        const scoreElement = document.getElementById('score');
        const gameOverElement = document.getElementById('gameOver');
        
        let score = 0;
        let gameRunning = false;
        let enemies = [];
        let bullets = [];
        let enemyBullets = [];
        let keys = {};
        let playerX = window.innerWidth / 2 - 25;
        let playerSpeed = 4; // 减少玩家速度
        let enemyInterval;
        let gameLoop;
        let enemyBulletInterval;
        
        // 初始化游戏
        function initGame() {
            score = 0;
            scoreElement.textContent = `分数: ${score}`;
            gameOverElement.style.display = 'none';
            clearAllEnemiesAndBullets();
            
            playerX = window.innerWidth / 2 - 25;
            player.style.left = `${playerX}px`;
            
            // 创建敌人
            enemyInterval = setInterval(createEnemy, 800); // 增加敌人生成频率
            
            // 敌人发射子弹
            enemyBulletInterval = setInterval(createEnemyBullet, 2000);
            
            // 游戏循环
            gameLoop = setInterval(updateGame, 20);
            
            gameRunning = true;
        }
        
        // 创建敌人
        function createEnemy() {
            if (!gameRunning) return;
            
            let enemyType = Math.random();
            let enemy, enemyObj;
            
            if (enemyType < 0.7) { // 普通敌人
                enemy = document.createElement('div');
                enemy.className = 'enemy';
                const xPos = Math.random() * (window.innerWidth - 40);
                enemy.style.left = `${xPos}px`;
                enemy.style.top = '0px';
                
                enemyObj = {
                    element: enemy,
                    x: xPos,
                    y: 0,
                    speed: 3 + Math.random() * 3, // 增加敌人速度
                    health: 1
                };
            } else if (enemyType < 0.9) { // 快速敌人
                enemy = document.createElement('div');
                enemy.className = 'enemy-fast';
                const xPos = Math.random() * (window.innerWidth - 30);
                enemy.style.left = `${xPos}px`;
                enemy.style.top = '0px';
                
                enemyObj = {
                    element: enemy,
                    x: xPos,
                    y: 0,
                    speed: 5 + Math.random() * 3, // 更快的敌人
                    health: 1
                };
            } else { // BOSS敌人
                enemy = document.createElement('div');
                enemy.className = 'enemy-boss';
                const xPos = Math.random() * (window.innerWidth - 60);
                enemy.style.left = `${xPos}px`;
                enemy.style.top = '0px';
                
                enemyObj = {
                    element: enemy,
                    x: xPos,
                    y: 0,
                    speed: 1 + Math.random() * 1, // 较慢但更难摧毁
                    health: 3
                };
            }
            
            gameArea.appendChild(enemy);
            enemies.push(enemyObj);
        }
        
        // 创建敌人子弹
        function createEnemyBullet() {
            if (!gameRunning) return;
            
            if (enemies.length > 0) {
                // 随机选择一个敌人发射子弹
                const randomEnemyIndex = Math.floor(Math.random() * enemies.length);
                const enemy = enemies[randomEnemyIndex];
                
                const bullet = document.createElement('div');
                bullet.className = 'enemy-bullet';
                const bulletX = enemy.x + 15;
                const bulletY = enemy.y + 40;
                bullet.style.left = `${bulletX}px`;
                bullet.style.top = `${bulletY}px`;
                
                gameArea.appendChild(bullet);
                
                enemyBullets.push({
                    element: bullet,
                    x: bulletX,
                    y: bulletY,
                    speed: 5
                });
            }
        }
        
        // 创建子弹
        function createBullet() {
            if (!gameRunning) return;
            
            const bullet = document.createElement('div');
            bullet.className = 'bullet';
            const bulletX = playerX + 22.5;
            const bulletY = window.innerHeight - 100;
            bullet.style.left = `${bulletX}px`;
            bullet.style.top = `${bulletY}px`;
            
            gameArea.appendChild(bullet);
            
            bullets.push({
                element: bullet,
                x: bulletX,
                y: bulletY,
                speed: 7
            });
        }
        
        // 更新游戏
        function updateGame() {
            if (!gameRunning) return;
            
            // 移动玩家
            if (keys['ArrowLeft'] && playerX > 0) {
                playerX -= playerSpeed;
            }
            if (keys['ArrowRight'] && playerX < window.innerWidth - 50) {
                playerX += playerSpeed;
            }
            player.style.left = `${playerX}px`;
            
            // 移动敌人
            enemies.forEach((enemy, index) => {
                enemy.y += enemy.speed;
                enemy.element.style.top = `${enemy.y}px`;
                
                // 移除超出屏幕的敌人
                if (enemy.y > window.innerHeight) {
                    gameArea.removeChild(enemy.element);
                    enemies.splice(index, 1);
                }
                
                // 检查碰撞
                if (
                    enemy.y + (enemy.element.className === 'enemy-boss' ? 60 : 40) > window.innerHeight - 100 &&
                    enemy.y < window.innerHeight - 50 &&
                    enemy.x + (enemy.element.className === 'enemy-boss' ? 60 : 40) > playerX &&
                    enemy.x < playerX + 50
                ) {
                    gameOver();
                }
            });
            
            // 移动子弹
            bullets.forEach((bullet, index) => {
                bullet.y -= bullet.speed;
                bullet.element.style.top = `${bullet.y}px`;
                
                // 移除超出屏幕的子弹
                if (bullet.y < 0) {
                    gameArea.removeChild(bullet.element);
                    bullets.splice(index, 1);
                }
                
                // 检查子弹与敌人的碰撞
                enemies.forEach((enemy, enemyIndex) => {
                    if (
                        bullet.y < enemy.y + (enemy.element.className === 'enemy-boss' ? 60 : 40) &&
                        bullet.y + 15 > enemy.y &&
                        bullet.x < enemy.x + (enemy.element.className === 'enemy-boss' ? 60 : 40) &&
                        bullet.x + 5 > enemy.x
                    ) {
                        // 减少敌人生命值
                        enemy.health--;
                        
                        // 移除子弹
                        gameArea.removeChild(bullet.element);
                        bullets.splice(index, 1);
                        
                        // 如果敌人生命值为0，移除敌人
                        if (enemy.health <= 0) {
                            gameArea.removeChild(enemy.element);
                            enemies.splice(enemyIndex, 1);
                            
                            // 根据敌人类型加分
                            if (enemy.element.className === 'enemy') {
                                score += 10;
                            } else if (enemy.element.className === 'enemy-fast') {
                                score += 15;
                            } else if (enemy.element.className === 'enemy-boss') {
                                score += 30;
                            }
                            scoreElement.textContent = `分数: ${score}`;
                        }
                    }
                });
            });
            
            // 移动敌人子弹
            enemyBullets.forEach((bullet, index) => {
                bullet.y += bullet.speed;
                bullet.element.style.top = `${bullet.y}px`;
                
                // 移除超出屏幕的子弹
                if (bullet.y > window.innerHeight) {
                    gameArea.removeChild(bullet.element);
                    enemyBullets.splice(index, 1);
                }
                
                // 检查敌人子弹与玩家的碰撞
                if (
                    bullet.y < window.innerHeight - 50 &&
                    bullet.y + 15 > window.innerHeight - 100 &&
                    bullet.x < playerX + 50 &&
                    bullet.x + 5 > playerX
                ) {
                    // 玩家被击中
                    gameOver();
                }
            });
        }
        
        // 清除所有敌人和子弹
        function clearAllEnemiesAndBullets() {
            enemies.forEach(enemy => {
                if (enemy.element.parentNode) {
                    gameArea.removeChild(enemy.element);
                }
            });
            bullets.forEach(bullet => {
                if (bullet.element.parentNode) {
                    gameArea.removeChild(bullet.element);
                }
            });
            enemyBullets.forEach(bullet => {
                if (bullet.element.parentNode) {
                    gameArea.removeChild(bullet.element);
                }
            });
            enemies = [];
            bullets = [];
            enemyBullets = [];
            clearInterval(enemyInterval);
            clearInterval(gameLoop);
            clearInterval(enemyBulletInterval);
        }
        
        // 游戏结束
        function gameOver() {
            gameRunning = false;
            clearAllEnemiesAndBullets();
            gameOverElement.style.display = 'block';
        }
        
        // 键盘事件监听
        document.addEventListener('keydown', (e) => {
            keys[e.key] = true;
            
            // 空格键发射子弹
            if (e.key === ' ' && gameRunning) {
                createBullet();
            }
            
            // 空格键控制游戏开始/暂停
            if (e.key === ' ' && !gameRunning) {
                if (gameOverElement.style.display === 'block') {
                    // 重新开始游戏
                    initGame();
                } else {
                    // 暂停游戏
                    gameRunning = !gameRunning;
                    if (gameRunning) {
                        gameLoop = setInterval(updateGame, 20);
                        enemyInterval = setInterval(createEnemy, 800);
                        enemyBulletInterval = setInterval(createEnemyBullet, 2000);
                    } else {
                        clearInterval(gameLoop);
                        clearInterval(enemyInterval);
                        clearInterval(enemyBulletInterval);
                    }
                }
            }
        });
        
        document.addEventListener('keyup', (e) => {
            keys[e.key] = false;
        });
        
        // 初始提示
        alert("游戏说明:\n1. 使用左右箭头键控制飞船移动\n2. 按空格键发射子弹\n3. 击中敌人得分\n4. 撞到敌人或被敌人子弹击中游戏结束\n5. 按空格键开始/暂停游戏\n\n难度增加:\n- 敌人生成速度更快\n- 敌人种类更多（普通、快速、BOSS）\n- 敌人会发射子弹\n- 玩家移动速度降低");
    </script>
</body>
</html>